<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="/admin/header.jsp"%>

    <!-- 右侧主窗口  start-->
	<!-- 商品分类列表  start-->
	<div id="page-wrapper">
		<div class="row">
			<div class="col-lg-12">
				<h1 class="page-header">旅游线路管理</h1>
			</div>
			<!-- /.col-lg-12 -->
		</div>
		<!-- /.row -->

		<a href="#" class="btn btn-primary" data-toggle="modal" 
		           data-target="#newCateDialog" >新建</a>
		<div class="row">
			<div class="col-lg-12">

				<div class="panel panel-default">
					
					<!-- /.panel-heading -->
					
					
					
					<!--  <a class="btn btn-primary btn-xs href="${pageContext.request.contextPath }/admin/tripadd.action">新增</a>
					-->
					<table  class="table table-bordered table-striped">
					<thead>
					<tr>
						<th width="100">名称</th>
						<th width="100">缩略图</th>
						<th>类型</th>
						<th>描述</th>
						<th width="100">操作</th>
					</tr>
					</thead>
					<tbody>
					<c:forEach items="${triplist}" var="t">
					<tr>
						<td>${t.tripname }</td>
						<td><img src="${pageContext.request.contextPath }/${t.trippic }" width="50"/></td>
						<td>${t.cname }</td>
						<td>${t.tripdesc }</td>
						<td><a class="btn btn-primary btn-xs" href="#" data-toggle="modal" 
		           data-target="#editTripDialog" onclick="editTrip(${t.tripid})">编辑</a> 
						<a class="btn btn-primary btn-xs" href="#" onclick="deleteTrip(${t.tripid})">删除</a></td>
					</tr>
					</c:forEach> 
					</tbody>
					</table>
					
					

					<!-- /.panel-body -->
				</div>
				<!-- /.panel -->

			</div>
			<!-- /.col-lg-12 -->
		</div>
	</div>
	<!-- 商品分类列表  end-->
</div>
<!-- 创建商品分类模态框 -->
<div class="modal fade" id="newCateDialog" tabindex="-1" role="dialog"
	aria-labelledby="myModalLabel">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
				<h4 class="modal-title" id="myModalLabel">新建旅游线路</h4>
			</div>
			<div class="modal-body">
				<form class="form-horizontal" id="new_trip_form" method="post" enctype="multipart/form-data">
					<div class="form-group">
						<label for="new_cateName" class="col-sm-2 control-label">
						    线路名称
						</label>
						<div class="col-sm-10">
							<input type="text" class="form-control" id="new_cateName" placeholder="旅游线路名称" name="tripname" />
						</div>
					</div>
					<div class="form-group">
						<label for="new_cateFrom" style="float:left;padding:7px 15px 0 27px;">上级分类</label> 
						<div class="col-sm-10">
														
							<select name="cid" class="form-control" id="new_cateFrom">
								<c:forEach items="${catlist }" var="c">
									<option value="${c.cid }">${c.cname }</option>
								</c:forEach>
							</select>
						</div>
					</div>

					<div class="form-group">
						<label for="trip_pic" class="col-sm-2 control-label">
						   线路图片
						</label>
						<div class="col-sm-10">
							<input type="file" class="form-control" id="trip_pic" placeholder="旅游线路名称" name="trippic" />
						</div>
					</div>
					
					<div class="form-group">
						<label for="new_catedesc" class="col-sm-2 control-label">描述</label>
						<div class="col-sm-10">
							<textarea class="form-control" id="new_catedesc" name="tripdesc" rows="3">请填写线路描述</textarea>
							
						</div>
					</div>

				</form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				<button type="button" class="btn btn-primary" onclick="createTrip()">创建线路</button>
			</div>
		</div>
	</div>
</div>

<!-- 修改线路模态框 -->
<div class="modal fade" id="editTripDialog" tabindex="-1" role="dialog"
	aria-labelledby="myModalLabel">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
				<h4 class="modal-title" id="myModalLabel">编辑旅游线路</h4>
			</div>
			<div class="modal-body">
				<form class="form-horizontal" id="edit_trip_form" method="post" enctype="multipart/form-data">
					<input type="hidden" id="edit_tripid" name="tripid" />
					<div class="form-group">
						<label for="edit_tripName" class="col-sm-2 control-label">
						    线路名称
						</label>
						<div class="col-sm-10">
							<input type="text" class="form-control" id="edit_tripName" placeholder="旅游线路名称" name="tripname" />
						</div>
					</div>
					<div class="form-group">
						<label for="edit_cid" style="float:left;padding:7px 15px 0 27px;">上级分类</label> 
						<div class="col-sm-10">
														
							<select name="cid" class="form-control" id="edit_cid">
								<c:forEach items="${catlist }" var="c">
									<option value="${c.cid }">${c.cname }</option>
								</c:forEach>
							</select>
						</div>
					</div>
					
					<div class="form-group">
						<label for="edit_tripdesc" class="col-sm-2 control-label">描述</label>
						<div class="col-sm-10">
							<textarea class="form-control" id="edit_tripdesc" name="tripdesc" rows="3">请填写线路描述</textarea>
							
						</div>
					</div>

				</form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				<button type="button" class="btn btn-primary" onclick="updateTrip()">编辑线路</button>
			</div>
		</div>
	</div>
</div>

<!-- 编写js代码 -->
<script type="text/javascript">
//清空新建商品分类窗口中的数据
	function clearCate() {
	    $("#new_cateName").val("");
	    $("#new_catedesc").val("")

	}
	// 创建商品分类
	function createTrip() {
	/*alert("ok");
	$.post("${pageContext.request.contextPath}/admin/addtripmodal.action",
			
	$("#new_trip_form").serialize(),function(data){
	        if(data =="OK"){
	            alert("商品分类创建成功！");
	            window.location.reload();
	        }else{
	            alert("商品分类创建失败！");
	            window.location.reload();
	        }
	        
	    });*/
	    var form = new FormData(document.getElementById("new_trip_form"));
		 $.ajax({
	            //几个参数需要注意一下
	                type: "post",//方法类型
	                //dataType: "json",//预期服务器返回的数据类型
	                url: "${pageContext.request.contextPath}/admin/addtripmodal.action" ,//url
	                data:form,
	                processData:false,
	                contentType:false,
	                success: function (data) {
	                    console.log(data);//打印服务端返回的数据(调试用)
	                    if(data =="OK"){
	        	            alert("商品分类创建成功！");
	        	            window.location.reload();
	        	        }else{
	        	            alert("商品分类创建失败！");
	        	            window.location.reload();
	        	        }
	                },
	                error : function() {
	                    alert("异常！");
	                }
	            });
	
	}
	// 通过id获取修改的商品信息
	function editTrip(tripid) {
		
		$.post("${pageContext.request.contextPath}/admin/tripeditmodal.action",{"tripid":tripid},function(data){
			 $("#edit_tripid").val(data.tripid);
	         $("#edit_tripName").val(data.tripname);
	         $("#edit_tripdesc").val(data.tripdesc);
             
             $("select option[value='"+data.cid+"']").attr("selected", "selected");  
		});
	}
    // 执行修改商品分类操作
	function updateTrip() {
    	
		$.post("${pageContext.request.contextPath}/admin/updatetripmodal.action",$("#edit_trip_form").serialize(),function(data){
			if(data =="OK"){
				alert("線路信息更新成功！");
				window.location.reload();
			}else{
				alert("線路分类信息更新失败！");
				window.location.reload();
			}
		});
	}
	// 删除商品分类
	function deleteTrip(tripid) {
	    if(confirm('确实要删除该線路吗?')) {
	$.post("${pageContext.request.contextPath}/admin/deltripmodal.action",{"tripid":tripid},
	function(data){
	            if(data =="OK"){
	                alert("線路删除成功！");
	                window.location.reload();
	            }else{
	                alert("删除線路失败！");
	                window.location.reload();
	            }
	        });
	    }
	}
</script>


<%@include file="/admin/footer.jsp"%>